package hash;

import searchable.BinarySearchTree;
import searchable.PrintTreeVisitor;
import wrapper.Comparable;
import enumeration.Enumeration;
import main.LinkedList;

public class ChainedHashTable extends AbstractHashTable {
	protected BinarySearchTree[] array;

	public ChainedHashTable(int length) {
		array = new BinarySearchTree[length];
		for (int i = 0; i < length; ++i)
			array[i] = new BinarySearchTree();
	}

	@Override
	public int getLength() {
		return array.length;
	}

	public void purge() {
		for (int i = 0; i < getLength(); ++i)
			array[i].purge();
		count = 0;
	}

	public void insert(Comparable object) {
		array[h(object)].insert(object);
		++count;
	}

	public void withdraw(Comparable object) {
		array[h(object)].withdraw(object);
		--count;
	}

	public wrapper.Comparable find(wrapper.Comparable object) {
		BinarySearchTree ptr = array[h(object)];
		return ptr.find(object);
	}

	public void printHashTable() {
		for (int i = 0; i < getLength(); i++) {
			System.out.print("\n" + i + ": ");
			if (array[i] != null) {
				array[i].printVisitorTree(new PrintTreeVisitor());
				}
			}
		}
	}

